package com.ouyy.volunteerservice.volunteer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ouyy.volunteerservice.volunteer.domain.po.Volunteer;
import com.ouyy.volunteerservice.volunteer.mapper.VolunteerMapper;
import com.ouyy.volunteerservice.volunteer.service.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements VolunteerService {
    // 这里可以加入自定义的业务逻辑
    @Autowired
    private VolunteerMapper volunteerMapper; // 注入 MyBatis Mapper

    @Override
    public List<Volunteer> getByOrganizationId(String organizationId) {
        return list(new LambdaQueryWrapper<Volunteer>()
                .like(Volunteer::getOrganizationIds, organizationId));
    }


    public List<Map<String, Object>> getMonthlyVolunteerCounts() {
        // 从数据库中查询按月份统计的注册人数
        List<Map<String, Object>> result = volunteerMapper.countVolunteersByMonth();

        // 如果需要返回 ECharts 格式的结果，你可能需要分解返回的数据
        // 如：月份的数组，注册人数的数组
        List<Map<String, Object>> formattedResult = new ArrayList<>();

        for (Map<String, Object> entry : result) {
            formattedResult.add(entry);
        }

        return formattedResult;
    }

    @Override
    public List<Map<String, Object>> getServiceTime() {
        return volunteerMapper.selectMaps(
                new QueryWrapper<Volunteer>()
                        .select(
                                "SUM(CASE WHEN total_service_hours BETWEEN 0 AND 50 THEN 1 ELSE 0 END) AS '0-50小时'",
                                "SUM(CASE WHEN total_service_hours BETWEEN 51 AND 100 THEN 1 ELSE 0 END) AS '51-100小时'",
                                "SUM(CASE WHEN total_service_hours BETWEEN 101 AND 200 THEN 1 ELSE 0 END) AS '101-200小时'",
                                "SUM(CASE WHEN total_service_hours > 200 THEN 1 ELSE 0 END) AS '200小时以上'"
                        )
        );
    }


}
